/* ===================

AX2E (AXESCON XACML 2.0 Engine) is the Java authorization engine, which implements OASIS XACML 2.0 standard.
Copyright (C) 2007 AXESCON LLC

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


Contact: AXESCON LLC - info{at}axescon.com

 =================== */
/*
 * XML Type:  Environment
 * Namespace: urn:oasis:names:tc:xacml:2.0:context:schema:os
 * Java type: axescon.xacml.model.ctx.Environment
 *
 * Automatically generated - do not modify.
 */
package axescon.xacml.model.ctx.impl;

import axescon.xacml.model.ctx.Attribute;
import axescon.xacml.model.shared.attr.DateAttribute;
import axescon.xacml.model.shared.attr.DateTimeAttribute;
import axescon.xacml.model.shared.attr.TimeAttribute;

import java.util.*;

/**
 * An XML Environment(@urn:oasis:names:tc:xacml:2.0:context:schema:os).
 * <p/>
 * This is a complex type.
 */
public class EnvironmentImpl implements axescon.xacml.model.ctx.RequestPart {

    public static final String ATTR_ID_CURRENT_TIME = "urn:oasis:names:tc:xacml:1.0:environment:current-time";
    public static final String ATTR_ID_CURRENT_DATE = "urn:oasis:names:tc:xacml:1.0:environment:current-date";
    public static final String ATTR_ID_CURRENT_DATETIME = "urn:oasis:names:tc:xacml:1.0:environment:current-dateTime";

    DateTimeAttribute currDateTime;
    TimeAttribute currTime;
    DateAttribute currDate;

    private Attribute[] attributeArray = new Attribute[3];

    private Attribute[] stdAttributeArray = new Attribute[3];

    /**
     * Set<String>
     */
    private static Set stdAttrIds = new HashSet(3);


    static {
        stdAttrIds.add(ATTR_ID_CURRENT_DATE);
        stdAttrIds.add(ATTR_ID_CURRENT_DATETIME);
        stdAttrIds.add(ATTR_ID_CURRENT_TIME);

    }

    /*see axescon.xacml 2.0 spec ch. B.5058 8. Environment attributes

5064 This identifier indicates the current time at the context handler. In practice it is the time at which
5065 the request context was created. For this reason, if these identifiers appear in multiple places
5066 within a <Policy> or <PolicySet>, then the same value SHALL be assigned to each occurrence
5067 in the evaluation procedure, regardless of how much time elapses between the processing of the
5068 occurrences.

    */
    {

        Calendar cal = new GregorianCalendar();
        currTime = new TimeAttribute();
        currDate = new DateAttribute();
        currDateTime = new DateTimeAttribute();

        stdAttributeArray[0] = new AttributeImpl(new TimeAttribute[]{currTime}, ATTR_ID_CURRENT_TIME, TimeAttribute.dataType);

        stdAttributeArray[1] = new AttributeImpl(new DateAttribute[]{currDate}, ATTR_ID_CURRENT_DATE, DateAttribute.dataType);

        stdAttributeArray[2] = new AttributeImpl(new DateTimeAttribute[]{currDateTime}, ATTR_ID_CURRENT_DATETIME, DateTimeAttribute.dataType);

        attributeArray = (Attribute[]) stdAttributeArray.clone();

    }


    public EnvironmentImpl() {

    }


    /**
     * replaces standard times with supplied one
     */
    public void setAttributeArray(Attribute[] attributeArray) {
        if (attributeArray == null) return;
        ArrayList newAttrs = new ArrayList(attributeArray.length);

        boolean isTimeSet = false;
        boolean isDateSet = false;
        boolean isDateTimeSet = false;
        for (int i = 0; i < attributeArray.length; i++) {
            Attribute attr = attributeArray[i];
            newAttrs.add(attr);
            if (!isTimeSet && ATTR_ID_CURRENT_TIME.equals(attr.getAttributeId())) isTimeSet = true;
            if (!isDateSet && ATTR_ID_CURRENT_DATE.equals(attr.getAttributeId())) isDateSet = true;
            if (!isDateTimeSet && ATTR_ID_CURRENT_DATETIME.equals(attr.getAttributeId())) isDateTimeSet = true;
        }

        if (!isTimeSet) newAttrs.add(stdAttributeArray[0]);
        if (!isDateSet) newAttrs.add(stdAttributeArray[1]);
        if (!isDateTimeSet) newAttrs.add(stdAttributeArray[2]);

        this.attributeArray = (Attribute[]) newAttrs.toArray(new Attribute[newAttrs.size()]);

    }

    public Attribute[] getAttributeArray() {
        return attributeArray;
    }


}
